package net.libra.ui.base
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import net.libra.ui.event.BaseEvent;
	import net.libra.ui.event.ItemEvent;
	import net.libra.utils.ObjectPool;

	/**
	 * 表格类
	 * @author elvisjiang
	 */	
	public class BaseGrid extends BaseSprite
	{
		static public const NAVI_PX:uint = 0;
		static public const NAVI_LINE:uint = 1;
		static public const NAVI_PAGE:uint = 2;
		
		static public var VERTICAL_GRID:uint = 0;
		static public var HORIZONTAL_GRID:uint = 1;
		
		private var _width:uint;
		private var _height:uint;
		//行
		private var _rowCount:uint;
		//列
		private var _colCount:uint;
		private var _itemWidth:Number;
		private var _itemHeight:Number;
		//水平间隔
		private var _horizontalGap:Number;
		//垂直间隔
		private var _verticalGap:Number;
		//数据
		private var _source:Array = [];
		//item
		private var _pool:ObjectPool;
		private var _type:uint;
		//当前页数 初始化是第一页
		private var _currentPage:int = 1;
		//总页数
		private var _totalPages:int;
		private var _startVertical:uint = 0;
		private var _startHorizontal:uint = 0;
		private var _lastStart:int = -1;
		
		private var _selectedIndex:int = -1;
		
		private var temp:int;
		
		public function BaseGrid(itemRenderer:Class,rowCount:uint,colCount:uint,itemWidth:uint,itemHeight:uint,horizontalGap:Number,verticalGap:Number,type:uint)
		{
			_pool = new ObjectPool(itemRenderer);
			
			_rowCount = rowCount;
			_colCount = colCount;
			
			_itemWidth = itemWidth;
			_itemHeight = itemHeight;
			
			_width = colCount == 0 ? 0 : (colCount * itemWidth + (colCount - 1) * horizontalGap);
			_height = rowCount == 0 ? 0 : (rowCount * itemHeight + (rowCount - 1) * verticalGap);
			
			_horizontalGap = horizontalGap;
			_verticalGap = verticalGap;
			
			_type = type;
			_start = 0;
			_itemArray = [];
			mouseChildren = true;
		}
		/**
		 * 设置数据源
		 * @param value
		 */		
		public function set source(value:Array):void
		{
			_start = 0;
			_source = value||[];
			_lastStart = -1;
			setCurrentPage(_currentPage);
//			update();
		}
		override protected function render():void
		{
			removeAll();
			
			if(_type == VERTICAL_GRID)
			{
				updateVertical();
			}else
			{
				updateHorizontal();
			}
			dispatchEvent(new BaseEvent(BaseEvent.BASE_RENDER));
		}
		
	  	protected function _dataChanged(event:BaseEvent):void
		{
			var item:ItemRenderer = event.currentTarget as ItemRenderer;
			
			source[item.index] = item.data;
		}
	
		private var _start:int;
		protected function updateVertical():void
		{
			for(var i:uint = _start; i < source.length && i < getCurrentPage() * numsPerPage;i++)
			{
				var item:ItemRenderer = _pool.borrowObject() as ItemRenderer;
				if(!item)
				{
					throw new Error("itemRenderer必须实现IItemRenderer");
				}
				item.init();
//				item.destroy();
				
				item.data = source[_start];
				item.index = _start;
				item.selected = (_start == selectedIndex);
				
				_itemArray[i] = item;
				
				var temp:int = i % numsPerPage;
				//ITEM的位置
				var x:Number = temp % colCount * (itemWidth + horizontalGap);
				item.x = x;
				item.y = int(temp/colCount) * (itemHeight + verticalGap);
				
				
				addChild(item);
				item.addEventListener(BaseEvent.DATA_CHANGE,_dataChanged);
				_start++;
			}
		}
		protected function updateHorizontal():void
		{
			
		}
		public function getItemRendererAt(index:uint):ItemRenderer
		{
			return _itemArray[index];
		}
		/**
		 * 选择一个item 
		 */ 
		public function setSelect(value:int):void
		{
			temp = value;
			this.addEventListener(BaseEvent.BASE_RENDER,__selectedIndex);
		}
		
		protected function __selectedIndex(event:BaseEvent):void
		{
			if(selectedIndex == temp)
			{
				return;
			}
			if(!_itemArray || _itemArray.length == 0)
			{
				return;
			}
			_selectedIndex = temp;
			for(var i:int = 0;i<_itemArray.length;i++)
			{
				var item:ItemRenderer = getItemRendererAt(_selectedIndex);
				if(item){
					if(i == temp)
					{
						item.selected = true;
					}else{
						item.selected = false;
					}
				}
			}
		}
		/**
		 * 移除所有item,并回收对象池
		 */
		protected function removeAll():void
		{
			while(numChildren > 0)
			{
				var item:ItemRenderer = removeChildAt(0) as ItemRenderer;
				if(item)
				{
					item.removeEventListener(BaseEvent.DATA_CHANGE,_dataChanged);
					_pool.returnObject(item);
				}			
			}
		}
		override public function destroy():void
		{
			source.length = 0;
			
			removeAll();
			while(_pool.length > 0)
			{
				var item:ItemRenderer = _pool.borrowObject() as ItemRenderer;
				if(item)
				{
					item.data = null;
					item.destroy();
				}
				
			}
			_pool.destroy();
			_pool = null;
			super.destroy();
		}
		/**
		 * 得到总页数
		 */		
		public function getTotalPages():int
		{
			if(_source.length % numsPerPage == 0)
			{
				_totalPages = _source.length / numsPerPage ;
			}else{
				_totalPages = _source.length / numsPerPage + 1;
			}
			if(_totalPages == 0)
			{
				_totalPages = 1; 
			}
			return _totalPages;
		}
		/**
		 * 得到当前页数
		 */		
		public function getCurrentPage():int
		{
			return _currentPage;
		}
		/**
		 * 设置第几页
		 */		
		public function setCurrentPage(value:int):void
		{
//			if(value == 0 && this.getTotalPages() > 0)
//			{
//				_currentPage = 1;
//			}else{
//				_currentPage = value;
//			}
			_currentPage = value;
			_start = (_currentPage - 1) * numsPerPage;
			
			update();
			
		}
		public function get numsPerPage():int
		{
			return rowCount * colCount;
		}
		private var _itemArray:Array;
		
		public function get itemArray():Array
		{
			return _itemArray.concat();
		}
		
		/////////////////////////////////////////////////////////////////////////////
		/**
		 * 行数
		 */		
		public function get rowCount():Number
		{
			return _rowCount;
		}
		/**
		 * 列数
		 */		
		public function get colCount():Number
		{
			return _colCount;
		}
		override public function get width():Number
		{
			return _width;
		}
		override public function get height():Number
		{
			return _height;
		}
		public function get itemWidth():Number
		{
			return _itemWidth;
		}
		public function get itemHeight():Number
		{
			return _itemHeight;
		}
		public function get horizontalGap():Number
		{
			return _horizontalGap;
		}
		public function get verticalGap():Number
		{
			return _verticalGap;
		}
		public function get source():Array
		{
			return _source;
		}
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}
	}
}